package com.cy.study.util;


import com.cy.study.exception.BusinessException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @version v1.0.0
 * @explain
 * @date 2024/3/18 11:23
 */
public class PasswordEncodeUtil {

    public static String passwordEncode(String password,String salt){

        salt =  Base64.getEncoder().encodeToString(salt.getBytes());
        String seed = password+salt;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(seed.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println(passwordEncode("123456","cpms"));
    }

}
